<template>
  <div class="toolbar colorMain">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" style="width: 600px;margin: auto" label-width="80px" class="login-form" auto-complete="on" label-position="left">
      <h3 class="title">修改密码</h3>
      <el-form-item prop="current_password" label="旧密码">
        <el-input
          :type="pwdType"
          v-model.trim="loginForm.current_password"
          name="password"
          auto-complete="on"
          placeholder="密码"
        />
        <span class="show-pwd" @click="showPwd">
          <!--<svg-icon icon-class="eye" />-->
        </span>
      </el-form-item>
      <el-form-item prop="password" label="新密码">
        <el-input
          :type="pwdType"
          v-model.trim="loginForm.password"
          name="password"
          auto-complete="on"
          placeholder="密码"
          @keyup.enter.native="handleLogin" />
        <span class="show-pwd" @click="showPwd">
          <!--<svg-icon icon-class="eye" />-->
        </span>
      </el-form-item>
      <el-form-item prop="compassword" label="确认密码">
        <el-input
          v-model.trim="loginForm.compassword"
          :type="pwdType"
          name="compassword"
          auto-complete="on"
          placeholder="确认密码"
          @keyup.enter.native="handleLogin" />
        <span class="show-pwd" @click="showPwd2">
          <!--<svg-icon icon-class="eye" />-->
        </span>
      </el-form-item>
      <div style="display: flex; justify-content: space-around">
        <el-button :loading="loading" type="danger" style="width:30%;" @click.native.prevent="handleLogin">
          修改密码
        </el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
import { changePassword } from '@/api/login'
import { mapGetters } from 'vuex'

export default {
  name: 'ChangePS',
  data() {
    const validatePass = (rule, value, callback) => {
      if (value.length < 5) {
        callback(new Error('密码不能小于5位'))
      } else {
        callback()
      }
    }
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.loginForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        current_password: '',
        password: '',
        compassword: ''
      },
      loginRules: {
        current_password: [{ required: true, trigger: 'blur', validator: validatePass }],
        password: [{ required: true, trigger: 'blur', validator: validatePass }],
        compassword: [{ required: true, trigger: 'blur', validator: validatePass2 }]
      },
      loading: false,
      pwdType: 'password',
      pwdType2: 'password',
      redirect: undefined
    }
  },
  computed: {
    ...mapGetters([
      'user_id'
    ])
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },

  methods: {
    showPwd() {
      if (this.pwdType === 'password') {
        this.pwdType = ''
      } else {
        this.pwdType = 'password'
      }
    },
    showPwd2() {
      if (this.pwdType2 === 'password') {
        this.pwdType = ''
      } else {
        this.pwdType2 = 'password'
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          // debugger
          const data = {
            password: this.loginForm.password,
            current_password: this.loginForm.current_password
          }
          changePassword(this.user_id, data).then(() => {
            this.loading = false
            this.$message({ message: '修改成功，新密码生效', type: 'success' })
            setTimeout(() => {
              this.$store.dispatch('LogOut').then(() => {
                location.reload() // 为了重新实例化vue-router对象 避免bug
              })
            }, 1000)
          }, err => {
            this.loading = false
            this.$message({ type: 'error', message: err.response.data.msg })
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
